package com.nike.amoeba.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.TimeUtils
import com.nike.amoeba.R
import com.nike.amoeba.base.MyBaseViewModel
import com.nike.amoeba.bean.AppRoomDataBase
import com.nike.amoeba.bean.db.StockEpcBean
import com.nike.amoeba.bean.db.TaskStockListBean
import com.nike.amoeba.bean.db.UploadOrderBean
import com.nike.amoeba.ext.showMessage
import com.nike.amoeba.ui.frg.TaskStockListFrg
import com.nike.amoeba.util.CacheUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField

/**
 * @Author nike
 * @Date 2023/7/7 12:00
 * @Description
 */
class TaskStockListModel: MyBaseViewModel() {

    var state = StringObservableField()

    var save = StringObservableField()

    var listBean: MutableLiveData<List<TaskStockListBean>> = MutableLiveData()

    var listStockEpcBean: MutableLiveData<ArrayList<StockEpcBean>> = MutableLiveData()

    val RoNo = CacheUtil.getUser()?.RoNo



    var isShowDialog: MutableLiveData<Boolean> = MutableLiveData()

    fun onRequest(orderId: String?, code: String?) {
        viewModelScope.launch(Dispatchers.IO) {
            if (RoNo != null) {
                val list = taskStockListDao.findAll(orderId!!, RoNo, companyId!!, code)
                list.forEach {
                    var epcAllList = stockEpcDao.findAll(it.RoNo!!, code)
                    it.ScanNum = epcAllList.size
                }
                val sortedList = list.sortedBy { it.ScanNum }
                listBean.postValue(ArrayList(sortedList))
            }
        }
    }

    fun onRequestEpc(orderId: String?, code: String?) {
        viewModelScope.launch(Dispatchers.IO) {
            val list = stockEpcDao.findAll(orderId!!, code)
            listStockEpcBean.postValue(list as ArrayList<StockEpcBean>)
        }
    }

    fun onSave(orderId: String?, frg: TaskStockListFrg, code: String?) {
        val time = TimeUtils.getNowString()
        viewModelScope.launch(Dispatchers.IO) {
            val list = stockEpcDao.findOrderIdAll(orderId!!)
            if (list.isNullOrEmpty()) {
                withContext(Dispatchers.Main) {
                    frg.showMessage(message = frg.getString(R.string.no_data))
                }
                return@launch
            }
            isShowDialog.postValue(true)

            var uploadOrderBean = uploadOrderDao.findOrderId(orderId, RoNo, companyId)
            if (uploadOrderBean == null){
                uploadOrderBean = UploadOrderBean()
            }
            uploadOrderBean.status = 0
            uploadOrderBean.code = code
            uploadOrderBean.title = "Stock Take：" + orderId + " | " + time
            uploadOrderBean.userId = RoNo!!
            uploadOrderBean.companyId = companyId!!
            uploadOrderBean.orderId = orderId!!
            uploadOrderDao.insertOrUpdate(uploadOrderBean)

            /*val findAll = stockEpcDao.findOrderIdAll(orderId)
            findAll.forEach {
                var bean = uploadOrderListDao.find(it.epc)
                if (bean == null){
                    bean = UploadOrderListBean()
                }
                bean.EPC = it.epc!!
                bean.ScanDate = time
                bean.orderNo = orderId
                bean.loginID = RoNo
                bean.companyId = companyId
                uploadOrderListDao.add(bean)
            }*/
            withContext(Dispatchers.Main) {
                runBlocking {
                    delay(1000) // 延迟1秒钟
                    isShowDialog.value = false
                }
                frg.showMessage(message = frg.getString(R.string.release_success))
            }
        }
    }

}